package com.zx.cashiersystem.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * SharedPreferences工具类
 */
public class SPUtils
{
	private static final String TAG = "SPUtils";
	public static final String USER_INFO = "userInfo";
	public static final String IS_LOGIN = "login";
	public static final String STORE_ID = "store_id";
	public static final String NEED_JIAOJIE = "need_jiaojie";


	/**
	 * 判断是否登录
	 * @param context
	 * @return
     */
	public static boolean isLogin(Context context) {
		SharedPreferences sp = context.getSharedPreferences(USER_INFO, context.MODE_PRIVATE);

		return sp.getBoolean(IS_LOGIN, false);

	}
	/**
	 * 判断是否登录
	 * @param context
	 * @return
	 */
	public static boolean isNeedJiaojie(Context context) {
		SharedPreferences sp = context.getSharedPreferences(USER_INFO, context.MODE_PRIVATE);

		return sp.getBoolean(NEED_JIAOJIE, false);

	}
	/**
	 * 获取保存的信息（String)
	 * @param context
	 * @param key
     * @return
     */
	public static String getUserinfo(Context context, String key) {
		SharedPreferences sp = context.getSharedPreferences(USER_INFO, context.MODE_PRIVATE);

		return sp.getString(key,null);
	}

	/**
	 * 获取保存的信息(int)
	 * @param context
	 * @param key
	 * @return
	 */
	public static int getIntUserinfo(Context context, String key) {
		SharedPreferences sp = context.getSharedPreferences(USER_INFO, context.MODE_PRIVATE);

		return sp.getInt(key,-1);
	}
	/**
	 * 获取保存的信息(boolean)
	 * @param context
	 * @param key
	 * @return
	 */
	public static boolean getBoolranUserinfos(Context context, String key) {
		SharedPreferences sp = context.getSharedPreferences(USER_INFO, context.MODE_PRIVATE);

		return sp.getBoolean(key,false);
	}
	/**
	 * 保存信息
	 * @param context
	 * @param key
	 * @param value
     */
	public static void setUserinfo(Context context, String key, Object value){
		SharedPreferences sp = context.getSharedPreferences(USER_INFO, context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sp.edit();
		if (value instanceof String)
		editor.putString(key,(String)value);
		else if (value instanceof Integer)
			editor.putInt(key,(int)value);
		else if (value instanceof Long)
			editor.putLong(key,(long)value);
		else if (value instanceof Boolean){
			editor.putBoolean(key,(boolean)value);
		}
		editor.commit();

	}

	/**
	 * 清空
	 * @param context
     */
	public static void clear(Context context){
		SharedPreferences sp = context.getSharedPreferences(USER_INFO, context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sp.edit();
		editor.clear();
		editor.commit();
	}



}